package sz.szsmk.citizencard.gover;

import android.content.Context;
import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.util.ACache;


public class FunctionAdapter extends RecyclerView.Adapter {

    private List<FunctionItem> data = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;

    public FunctionAdapter(Context context, @NonNull List<FunctionItem> data) {
        this.context = context;
        if (data != null) {
            this.data = data;
        }
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (0 == viewType) {
            holder = new TitleViewHolder(inflater.inflate(R.layout.layout_function_text, parent, false));
        } else {
            holder = new FunctionViewHolder(inflater.inflate(R.layout.layout_grid_item, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,final  int position) {

        if (0 == getItemViewType(position)) {
            TitleViewHolder holder = (TitleViewHolder) viewHolder;
//            holder.text.setText(data.get(position).name);
            if(position==0){
                holder.line.setVisibility(View.GONE);
            }else{
                 holder.line.setVisibility(View.VISIBLE);
            }

            holder.text.setVisibility(View.VISIBLE);
            holder.text.setText(data.get(position).name);


//            Glide.with(context)
//                    .load(data.get(position).getImageUrl())
//                    .into(holder.);
        } else {


            final int index = position;
            final FunctionViewHolder holder = (FunctionViewHolder) viewHolder;
            final FunctionItem fi = data.get(position);
            final boolean isClick = data.get(position).isClick;
//            boolean isSelect = data.get(position).isSelect;
            if (isClick) {
                holder.btn.setVisibility(View.GONE);
                holder.item.setBackgroundColor(Color.WHITE);
            } else {
                holder.btn.setVisibility(View.VISIBLE);
                holder.item.setBackgroundColor(Color.parseColor("#F2F6FA"));
            }
            //setImage(fi.imageUrl, holder.iv);
            holder.text.setText(fi.name);
            Glide.with(context)
                    .load(data.get(position).getImageUrl())
                    .into(holder.iv);

//            if(position<=5){
//                holder.btn.setVisibility(View.GONE);
//            }else{

//            String fiveFun= ACache.get(context).getAsString("fiveFun");
//            if(fiveFun.contains(data.get(position).name)){
//                Log.d("clark",data.get(position).name+fiveFun);
//                holder.btn.setVisibility(View.GONE);
//            }



            holder.btn.setImageResource(fi.isSelect ? R.drawable.ic_block_delete : R.drawable.ic_block_add);
            holder.btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
//                    holder.btn.setVisibility(View.GONE);

                    holder.item.setBackgroundColor(Color.parseColor("#00000000"));
                    FunctionItem f = data.get(index);
//                    Toast.makeText(context, "11111" + f.test + f.name, Toast.LENGTH_SHORT).show();
//                    Toast.makeText(context, "22222" + f.getImageUrl() + f.name, Toast.LENGTH_SHORT).show();
                    if (!f.isSelect) {
                        if (listener != null) {
                            if (listener.add(f)) {
                                f.isSelect = true;
                                notifyDataSetChanged();
                            }
                        }
                    }else{
                        if (reduceListener != null) {
                            if (reduceListener.reduce(f)) {
                                f.isSelect = false;
                                notifyDataSetChanged();
                            }
                        }
                    }
                }
            });
//            if(isClick){
                holder.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ghfInterface != null) {
                            ghfInterface.onClick(v,data.get(position), position,isClick);
                        }
                    }
                });
//            }
//            }
        }
    }

    public void setImage(String url, ImageView iv) {
        try {
            int rid = context.getResources().getIdentifier(url, "drawable", context.getPackageName());
            iv.setImageResource(rid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).isTitle ? 0 : 1;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setViewData(List<FunctionItem> viewData) {
        this.data = viewData;
    }


    private class TitleViewHolder extends RecyclerView.ViewHolder {

        private TextView text;
        private View line;
        public TitleViewHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.text);
            line= itemView.findViewById(R.id.line);
        }
    }

    private class FunctionViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv, btn;
        private TextView text;
        private final SquareRelativeLayout item;

        public FunctionViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            text = (TextView) itemView.findViewById(R.id.text);
            btn = (ImageView) itemView.findViewById(R.id.btn);
            item = (SquareRelativeLayout) itemView.findViewById(R.id.item);


        }
    }
    public interface GhfInterface {
        public void onClick(View view,FunctionItem data, int position,boolean isClick);
    }

    public void GhfOnClick(FunctionAdapter.GhfInterface ghfInterface) {
        this.ghfInterface = ghfInterface;
    }

    private GhfInterface ghfInterface;
    public interface OnItemAddListener {
        boolean add(FunctionItem item);
    }
    public interface OnItemReduceListener {
        boolean reduce(FunctionItem item);
    }

    private OnItemAddListener listener;
    private OnItemReduceListener reduceListener;
    public void setOnItemAddListener(OnItemAddListener listener) {
        this.listener = listener;
    }
    public void setOnItemReduceListener(OnItemReduceListener reduceListener) {
        this.reduceListener = reduceListener;
    }
}
